<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程同步机制详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .code-container {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            transition: transform 0.3s ease;
        }
        
        .code-container:hover {
            transform: translateY(-5px);
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .keyword { color: #569cd6; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .number { color: #b5cea8; }
        .class-name { color: #4ec9b0; }
        
        .feature-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .feature-card:hover {
            transform: translateY(-10px);
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
            border-color: #667eea;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 30px;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .result-box {
            background: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            font-family: monospace;
            margin-top: 20px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-sync-alt mr-4"></i>Java线程同步机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解同步方法与同步代码块的实现原理</p>
            <div class="flex justify-center gap-6 text-lg">
                <span><i class="fas fa-code mr-2"></i>代码示例</span>
                <span><i class="fas fa-shield-alt mr-2"></i>线程安全</span>
                <span><i class="fas fa-tachometer-alt mr-2"></i>性能优化</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="feature-card mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>概述
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">线</span>程同步是Java并发编程中的核心概念，用于解决多线程环境下的数据竞争问题。Java提供了两种主要的同步机制：<strong class="text-purple-600">同步方法</strong>和<strong class="text-purple-600">同步代码块</strong>。本文将通过一个经典的售票系统案例，深入探讨这两种同步机制的实现原理和使用场景。
            </p>
        </div>

        <!-- Problem Introduction -->
        <div class="feature-card mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-exclamation-triangle mr-3 text-yellow-500"></i>问题的引出
            </h2>
            <p class="text-lg text-gray-700 mb-6">
                让我们先看一个典型的多线程并发问题：四个售票窗口同时销售五张票。
            </p>
            
            <div class="code-container">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="text-gray-400 ml-4">Temp.java</span>
                </div>
                <pre><code><span class="keyword">public class</span> <span class="class-name">Temp</span> {
     <span class="keyword">public static void</span> main(String[] args) {
           
           <span class="class-name">Demo</span> dome = <span class="keyword">new</span> <span class="class-name">Demo</span>();
           
            <span class="keyword">new</span> Thread(dome).start();
            <span class="keyword">new</span> Thread(dome).start();
            <span class="keyword">new</span> Thread(dome).start();
            <span class="keyword">new</span> Thread(dome).start(); <span class="comment">// 四个买票点去买五张票</span>
     }
}

<span class="keyword">class</span> <span class="class-name">Demo</span> <span class="keyword">implements</span> <span class="class-name">Runnable</span>{
     <span class="keyword">private int</span> ticket = <span class="number">5</span>; <span class="comment">// 总共五张票</span>
     
     @Override
     <span class="keyword">public void</span> run() {
            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {
                 <span class="keyword">if</span> (ticket > <span class="number">0</span>) {
                      <span class="keyword">try</span> {
                           Thread.sleep(<span class="number">100</span>);
                     } <span class="keyword">catch</span> (InterruptedException e) {
                           e.printStackTrace();
                     }
                     System.out.println(<span class="string">"卖票："</span> + ticket--);
                }
           }
     }
}</code></pre>
            </div>
            
            <div class="result-box">
                <h4 class="font-bold text-gray-700 mb-2"><i class="fas fa-terminal mr-2"></i>运行结果：</h4>
                <div class="text-red-600">
                    卖票：5<br>
                    卖票：4<br>
                    卖票：3<br>
                    卖票：2<br>
                    卖票：1<br>
                    <strong>卖票：0</strong><br>
                    <strong>卖票：-1</strong><br>
                    <strong>卖票：-2</strong>
                </div>
            </div>
            
            <div class="mt-6 p-4 bg-red-50 border-l-4 border-red-500 rounded">
                <p class="text-gray-700">
                    <i class="fas fa-info-circle mr-2 text-red-500"></i>
                    <strong>问题分析：</strong>出现负数说明当一个线程正在执行减操作时，另一个线程已经将票数减少了。这是典型的<strong>竞态条件</strong>问题，需要使用同步机制来解决。
                </p>
            </div>
        </div>

        <!-- Synchronization Methods -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <!-- Synchronized Method -->
            <div class="feature-card">
                <h3 class="text-2xl font-bold text-gray-800 mb-4">
                    <i class="fas fa-lock mr-2 text-blue-600"></i>同步方法
                </h3>
                <p class="text-gray-700 mb-4">
                    通过在方法声明中添加 <code class="bg-gray-100 px-2 py-1 rounded">synchronized</code> 关键字，可以将整个方法设置为同步方法。
                </p>
                
                <div class="code-container">
                    <div class="code-header">
                        <div class="code-dot bg-red-500"></div>
                        <div class="code-dot bg-yellow-500"></div>
                        <div class="code-dot bg-green-500"></div>
                        <span class="text-gray-400 ml-4">同步方法示例</span>
                    </div>
                    <pre><code><span class="keyword">class</span> <span class="class-name">Demo</span> <span class="keyword">implements</span> <span class="class-name">Runnable</span>{
     <span class="keyword">private int</span> ticket = <span class="number">5</span>;
     
     @Override
     <span class="keyword">public synchronized void</span> run() {
            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {
                 <span class="keyword">if</span> (ticket > <span class="number">0</span>) {
                      <span class="keyword">try</span> {
                           Thread.sleep(<span class="number">100</span>);
                     } <span class="keyword">catch</span> (InterruptedException e) {
                           e.printStackTrace();
                     }
                     System.out.println(<span class="string">"卖票："</span> + ticket--);
                }
           }
     }
}</code></pre>
                </div>
            </div>

            <!-- Synchronized Block -->
            <div class="feature-card">
                <h3 class="text-2xl font-bold text-gray-800 mb-4">
                    <i class="fas fa-code mr-2 text-green-600"></i>同步代码块
                </h3>
                <p class="text-gray-700 mb-4">
                    使用 <code class="bg-gray-100 px-2 py-1 rounded">synchronized</code> 代码块可以更精确地控制同步范围，提高性能。
                </p>
                
                <div class="code-container">
                    <div class="code-header">
                        <div class="code-dot bg-red-500"></div>
                        <div class="code-dot bg-yellow-500"></div>
                        <div class="code-dot bg-green-500"></div>
                        <span class="text-gray-400 ml-4">同步代码块示例</span>
                    </div>
                    <pre><code><span class="keyword">class</span> <span class="class-name">Demo</span> <span class="keyword">implements</span> <span class="class-name">Runnable</span> {
     <span class="keyword">private int</span> ticket = <span class="number">5</span>;

     @Override
     <span class="keyword">public void</span> run() {
            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {
                 <span class="keyword">synchronized</span> (<span class="keyword">this</span>) {
                      <span class="keyword">if</span> (ticket > <span class="number">0</span>) {
                            <span class="keyword">try</span> {
                                Thread.sleep(<span class="number">100</span>);
                           } <span class="keyword">catch</span> (InterruptedException e) {
                                e.printStackTrace();
                           }
                           System.out.println(<span class="string">"卖票："</span> + ticket--);
                     }
                }
           }
     }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Result After Synchronization -->
        <div class="feature-card mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-check-circle mr-3 text-green-600